package org.example.service.Impl;

import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.metadata.Sheet;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.codec.digest.DigestUtils;
import org.example.commonapi.domain.Course;
import org.example.commonapi.domain.ExcelListener;
import org.example.commonapi.domain.ExcelUser;
import org.example.commonapi.domain.Practice;
import org.example.commonapi.domain.R;
import org.example.commonapi.domain.School;
import org.example.commonapi.domain.SchoolAccount;
import org.example.commonapi.domain.User;
import org.example.commonapi.domain.Video;
import org.example.commonapi.dto.LoginInfo;
import org.example.commonapi.dto.SchoolAccountDto;
import org.example.commonapi.mapper.CourseMapper;
import org.example.commonapi.mapper.PracticeMapper;
import org.example.commonapi.mapper.SchoolAccountMapper;
import org.example.commonapi.mapper.SchoolMapper;
import org.example.commonapi.mapper.UserMapper;
import org.example.commonapi.mapper.VideoMapper;
import org.example.commonapi.util.JwtUtil;
import org.example.commonapi.util.redis.RedisService;
import org.example.commonapi.util.redis.StatisticPrefix;
import org.example.commonapi.vo.CourseVO;
import org.example.commonapi.vo.SchoolAccountVO;
import org.example.commonapi.vo.StatisticVO;
import org.example.commonapi.vo.VideoVO;
import org.example.service.SchoolAccountService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import static java.time.format.DateTimeFormatter.ofPattern;

@Service
public class SchoolAccountServiceImpl extends ServiceImpl<SchoolAccountMapper, SchoolAccount> implements SchoolAccountService {

    @Resource
    private JwtUtil jwtUtil;

    @Autowired
    private SchoolMapper schoolMapper;

    @Autowired
    private SchoolAccountMapper schoolAccountMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private CourseMapper courseMapper;

    @Autowired
    private VideoMapper videoMapper;

    @Autowired
    private PracticeMapper practiceMapper;

    @Resource
    private RedisService redisService;

    @Override
    public R submitInformation(SchoolAccountDto schoolAccountDto, HttpServletRequest request) {
        Long userId = jwtUtil.parseJwt(request.getHeader("token")).getId();
        LambdaQueryWrapper<School> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(School::getSchoolName, schoolAccountDto.getSchoolName());
        School school = schoolMapper.selectOne(lambdaQueryWrapper);
        LambdaQueryWrapper<SchoolAccount> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SchoolAccount::getSchoolId, school.getId());
        queryWrapper.eq(SchoolAccount::getUserId, userId);
        // 先查询 看是否存在 如果存在转换为更新操作
        SchoolAccount existSchoolAccount = schoolAccountMapper.selectOne(queryWrapper);
        if ( existSchoolAccount != null ) {
            existSchoolAccount.setSchoolId(school.getId());
            existSchoolAccount.setProofImg(schoolAccountDto.getProofImg());
            // 提交状态设置为 审核中
            existSchoolAccount.setStatus(1);
            schoolAccountMapper.updateById(existSchoolAccount);
            return R.success("提交成功");
        }
        SchoolAccount schoolAccount = new SchoolAccount();
        schoolAccount.setSchoolId(school.getId());
        schoolAccount.setUserId(userId);
        // 提交状态设置为 审核中
        schoolAccount.setStatus(1);
        schoolAccount.setProofImg(schoolAccountDto.getProofImg());
        schoolAccountMapper.insert(schoolAccount);
        return R.success("提交成功");
    }

    @Override
    public R<List<SchoolAccountVO>> getSchoolCheckInformation() {
        List<SchoolAccount> schoolAccounts = schoolAccountMapper.selectList(null);
        List<SchoolAccountVO> schoolAccountVOS = schoolAccounts.stream().map(schoolAccount -> {
            // 构造VO返回值
           SchoolAccountVO schoolAccountVO = new SchoolAccountVO();
           LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
           userLambdaQueryWrapper.eq(User::getId, schoolAccount.getUserId());
           User user = userMapper.selectOne(userLambdaQueryWrapper);
           LambdaQueryWrapper<School> schoolLambdaQueryWrapper = new LambdaQueryWrapper<>();
           schoolLambdaQueryWrapper.eq(School::getId, schoolAccount.getSchoolId());
           School school = schoolMapper.selectOne(schoolLambdaQueryWrapper);
           schoolAccountVO.setId(schoolAccount.getId());
           schoolAccountVO.setSchoolName(school.getSchoolName());
           schoolAccountVO.setUsername(user.getUsername());
           schoolAccountVO.setStatus(schoolAccount.getStatus());
           schoolAccountVO.setProofImg(schoolAccount.getProofImg());
           schoolAccountVO.setReason(schoolAccount.getReason());
           return schoolAccountVO;
        }).collect(Collectors.toList());
        return R.success(schoolAccountVOS);
    }

    @Override
    public R passSchoolCheckInformation(Integer id) {
        LambdaQueryWrapper<SchoolAccount> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SchoolAccount::getId, id);
        SchoolAccount schoolAccount = schoolAccountMapper.selectOne(lambdaQueryWrapper);

        // 修改状态为 通过
        schoolAccount.setStatus(2);
        // 修改不通过意见为空
        schoolAccount.setReason("");
        schoolAccountMapper.updateById(schoolAccount);
        return R.success("审核通过");
    }

    @Override
    public R refuseSchoolCheckInformation(Integer id,String reason) {
        LambdaQueryWrapper<SchoolAccount> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SchoolAccount::getId, id);
        SchoolAccount schoolAccount = schoolAccountMapper.selectOne(lambdaQueryWrapper);

        // 修改状态为 不通过
        schoolAccount.setStatus(0);
        schoolAccount.setReason(reason);
        schoolAccountMapper.updateById(schoolAccount);
        return R.success("审核未通过");
    }

    @Override
    public R<SchoolAccount> getNowUserSubmitInfo(HttpServletRequest request) {
        Long userId = jwtUtil.parseJwt(request.getHeader("token")).getId();
        LambdaQueryWrapper<SchoolAccount> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SchoolAccount::getUserId, userId);
        SchoolAccount schoolAccount = schoolAccountMapper.selectOne(lambdaQueryWrapper);
        if ( schoolAccount == null ) {
            // 前端根据返回的用户状态判断用户之前是否提交过证明材料
            SchoolAccount newSchoolAccount = new SchoolAccount();
            newSchoolAccount.setStatus(-1);
            return R.success(newSchoolAccount);
        }
        return R.success(schoolAccount);
    }
}
